package com.trailer.base.widget

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.NonNull
import androidx.core.content.res.ResourcesCompat
import com.qmuiteam.qmui.kotlin.onClick
import com.trailer.base.R

/**
 * Created by xianguangjin on 16/6/1.
 * 自定义
 */
class TitleBar : RelativeLayout {
    private lateinit var dataSource: DataSource
    var imgLeft: ImageView? = null
        private set
    var tvTitle: TextView? = null
        private set

    /**
     * 隐藏未读jiaobiao
     *
     * @param
     */
    var tvRight: TextView? = null
        private set
    var imgRight: ImageView? = null
        private set
    var imgRightLeft: ImageView? = null
        private set
    var tvLeft: TextView? = null
        private set

    @JvmField
    var tv_title_bottom: TextView? = null

    constructor(context: Context, @NonNull dataSource: DataSource) : super(context) {
        this.dataSource = dataSource
        init(context, null, 0)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs, 0)
    }

    private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        inflate(context, R.layout.widget_title_bar, this)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar)
        val style = typedArray.getInt(R.styleable.TitleBar_title_bar_style, 0)
        val title = typedArray.getString(R.styleable.TitleBar_title_bar_title)
        val rightText = typedArray.getString(R.styleable.TitleBar_title_bar_right_text)
        val leftText = typedArray.getString(R.styleable.TitleBar_title_bar_left_text)
        val rightDrawable = typedArray.getDrawable(R.styleable.TitleBar_title_bar_right_img)
        val rightLeftDrawable = typedArray.getDrawable(R.styleable.TitleBar_title_bar_right_left_img)
        val leftImgEnabled = typedArray.getBoolean(R.styleable.TitleBar_title_bar_left_img_enabled, true)

        imgLeft = findViewById(R.id.img_back)
        tvTitle = findViewById(R.id.tv_title)
        tvRight = findViewById(R.id.tv_right)
        tvLeft = findViewById(R.id.tv_left)
        imgRight = findViewById(R.id.img_right)
        imgRightLeft = findViewById(R.id.img_right_left)

        tv_title_bottom = findViewById(R.id.tv_title_bottom)


        dataSource.title()?.apply {
            setBackgroundColor(ResourcesCompat.getColor(resources, dataSource.bgColor(), null))

            dataSource.backable().apply {
                if (this) {
                    setLeftImg(ResourcesCompat.getDrawable(resources, dataSource.backDrawable(), null))
                    imgLeft?.onClick {
                        (context as Activity).finish()
                    }
                } else {
                    imgLeft?.visibility = View.INVISIBLE
                }
            }


            if (!TextUtils.isEmpty(title)) {
                tvTitle?.visibility = VISIBLE
                setTitle(title)
            } else {
                tvTitle?.text = dataSource.title()
            }

            tvTitle?.setTextColor(ResourcesCompat.getColor(getContext().resources, dataSource.titleColor(), null))
            tvTitle?.textSize = dataSource.titleSize().toFloat()
            tvRight?.setTextColor(ResourcesCompat.getColor(getContext().resources, dataSource.rightTextColor(), null))

            if (!TextUtils.isEmpty(rightText)) {
                setRightText(rightText)
            }
            if (!TextUtils.isEmpty(leftText)) {
                setLeftText(leftText)
            }

            rightDrawable?.apply { setRitghtImg(this) }
            rightLeftDrawable?.apply { setRitghtLeftImg(this) }

        }


        typedArray.recycle()
    }

    /**
     * 设置右边的图片，并显示出来
     *
     * @param drawable
     */
    fun setRitghtImg(drawable: Drawable?) {
        imgRight?.visibility = VISIBLE
        imgRight?.setImageDrawable(drawable)
    }

    /**
     * 设置左边的图片，并显示出来
     *
     * @param drawable
     */
    fun setLeftImg(drawable: Drawable?) {
        imgLeft?.visibility = VISIBLE
        imgLeft?.setImageDrawable(drawable)
    }

    /**
     * 设置右边靠左的图片，并显示出来
     *
     * @param drawable
     */
    fun setRitghtLeftImg(drawable: Drawable?) {
        imgRightLeft!!.visibility = VISIBLE
        imgRightLeft!!.setImageDrawable(drawable)
    }

    /**
     * 设置右边textview的文字，并显示出来
     *
     * @param rightText
     */
    fun setRightText(rightText: String?) {
        tvRight!!.visibility = VISIBLE
        tvRight!!.text = rightText
    }

    /**
     * 设置左边textview的文字，并显示出来
     *
     * @param leftText
     */
    fun setLeftText(leftText: String?) {
        tvLeft!!.visibility = VISIBLE
        tvLeft!!.text = leftText
    }

    /**
     * 显示未读jiaobiao
     *
     * @param
     */
    fun setTitle(title: String?) {
        tvTitle!!.text = title
    }

    fun setTitle(ResId: Int) {
        tvTitle!!.text = context.getString(ResId)
    }


    interface DataSource {
        /**
         * @return title==null的时候不显示titlebar
         */
        fun title(): String?
        fun backable(): Boolean

        @ColorRes
        fun titleColor(): Int

        @ColorRes
        fun rightTextColor(): Int

        fun titleSize(): Int
        fun transluent(): Boolean

        @ColorRes
        fun bgColor(): Int {
            return R.color.colorPrimary
        }

        @DrawableRes
        fun backDrawable(): Int {
            return R.drawable.ic_back
        }
    }
}
